
//
//  LzwSearchController.m
//  NewHalfsugar
//
//  Created by 刘志武 on 16/1/2.
//  Copyright © 2016年 刘志武. All rights reserved.
//

#import "LzwSearchController.h"
#import "Block.h"
#import "LzwSearchCollectCell.h"
#import "LzwSeaDetailVC.h"
#import "LzwbaseCollectView.h"
#import "MBProgressHUD.h"

@interface LzwSearchController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) LzwbaseCollectView *mySearchCollectView;

@property (nonatomic, retain) MBProgressHUD *HUD;

@end

@implementation LzwSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    
    self.navigationItem.title = @"分类";
    
    UIImage * returnPopPage = [UIImage imageNamed:@"return.png"];
    returnPopPage = [returnPopPage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:returnPopPage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
    
    UICollectionViewFlowLayout *myLayout = [[UICollectionViewFlowLayout alloc]init];
    
    myLayout.itemSize = CGSizeMake(80 * WIDTH, 80 * WIDTH);
    
    myLayout.sectionInset = UIEdgeInsetsMake(30 * WIDTH, 30 * WIDTH, 30 * WIDTH, 10 * WIDTH);
//    myLayout.sectionInset = UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
    myLayout.minimumInteritemSpacing = 10 * WIDTH;
    myLayout.minimumLineSpacing = 30 * WIDTH;
    
    myLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    
    
    self.mySearchCollectView = [[LzwbaseCollectView alloc]initWithFrame:[[UIScreen mainScreen]bounds] collectionViewLayout:myLayout];
//    self.mySearchCollectView.backgroundColor = [UIColor whiteColor];
    self.mySearchCollectView.dataSource = self;
    self.mySearchCollectView.delegate = self;
    
    [self.view addSubview:self.mySearchCollectView];
    
    [self.mySearchCollectView registerClass:[LzwSearchCollectCell class] forCellWithReuseIdentifier:@"cell"];
    
    self.arraySea = [NSMutableArray array];
    
    //小宋菊花
    //小宋葵花第二步
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.HUD show:YES];
    self.HUD.labelText = @"正在拼命加载..";
    [self.view addSubview:self.HUD];
    
    NSString *string = @"http://open3.bantangapp.com/category/scene";
    
    NSDictionary *dicSe = @{@"app_installtime":@"1451365479.933367", @"app_versions":@"5.1.1", @"channel_name":@"appStore", @"client_id":@"bt_app_ios", @"client_secret":@"9c1e6634ce1c5098e056628cd66a17a5", @"device_token":@"c02c60075e7bab110fbdf27e15de3cb61e97360c4ea3545fd8a5936e46c28ccb", @"oauth_token":@"a41d3848f43102aa7759c27ba2c0fe8f", @"os_versions":@"9.1", @"screensize":@"640", @"track_device_info":@"iPhone5%2C3", @"track_deviceid":@"A80585E3-D6AD-4A53-BE1C-5963901C96E5", @"track_user_id":@"1746436", @"v":@"9"};
    
    
    [Block creatingDataTask:string parameters:dicSe Cookie:nil User_Agent:nil successAFNBlock:^(id resuposeObject) {
        
        
        NSMutableArray *array = [resuposeObject objectForKey:@"data"];
        
        for (NSMutableDictionary *diction in array) {
            
            LzwSearchModle *modleSe = [[LzwSearchModle alloc]init];
            
            [modleSe setValuesForKeysWithDictionary:diction];
            
            [self.arraySea addObject:modleSe];
            
            
        }
        
        [self.mySearchCollectView reloadData];
        
        [self.HUD hide:NO];
    }];
    
   
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.arraySea.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwSearchCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.modleSea = [self.arraySea objectAtIndex:indexPath.row];
    
    return cell;
    
    
}

//点击item触发事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwSeaDetailVC *SeDetailVC = [[LzwSeaDetailVC alloc]init];
    
    
    SeDetailVC.modleSEA = [self.arraySea objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:SeDetailVC animated:YES];
    
    
}

//返回按钮触发事件
- (void)leftButtonAction:(UIBarButtonItem *)bar{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
